import * as Api from "../Api/index";
import * as Types from "./types";

const actions = {
  async vaildLogin({ dispatch, commit }) {
    try {
      dispatch(Types.SHOWLOADING);
      commit(Types.VAILD_LOGIN, (await Api.vaildLogin()) == "ok");
      dispatch(Types.HIDELOADING);
    } catch (error) {
      console.log(error);
    }
  },
  async getAllTicker({ dispatch, commit }) {
    try {
      dispatch(Types.SHOWLOADING);
      commit(Types.UPDATE_TICKER, {
        btc: await Api.getBtcData(),
        ltc: await Api.getLtcData(),
        eth: await Api.getEthData()
      });
      dispatch(Types.HIDELOADING);
    } catch (error) {
      console.log(error);
    }
  },
  async getBtcTicker({ dispatch, commit }) {
    try {
      dispatch(Types.SHOWLOADING);
      commit(Types.UPDATE_BTC, await Api.getBtcData());
      dispatch(Types.HIDELOADING);
    } catch (error) {
      console.log(error);
    }
  },
  async getLtcTicker({ dispatch, commit }) {
    try {
      dispatch(Types.SHOWLOADING);
      commit(Types.UPDATE_BTC, await Api.getLtcData());
      dispatch(Types.HIDELOADING);
    } catch (error) {
      console.log(error);
    }
  },
  async getEthTicker({ dispatch, commit }) {
    try {
      dispatch(Types.SHOWLOADING);
      commit(Types.UPDATE_BTC, await Api.getEthData());
      dispatch(Types.HIDELOADING);
    } catch (error) {
      console.log(error);
    }
  },
  [Types.SHOWLOADING]({ commit }) {
    commit(Types.SHOWLOADING);
  },
  [Types.HIDELOADING]({ commit }) {
    commit(Types.HIDELOADING);
  }
};

export default actions;
